home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / getrusag.c < prev    next >
C/C++ Source or Header  |  1993-10-11  |  1KB  |  74 lines

  1. /* getrusage emulation for MiNT */
  2.  
  3. #include <compiler.h>
  4. #include <osbind.h>
  5. #include <mintbind.h>
  6. #include <time.h>
  7. #include <resource.h>
  8. #include <errno.h>
  9.  
  10. extern int __mint;
  11. extern long _childtime;
  12.  
  13. __EXTERN void _bzero __PROTO((void *, unsigned long));
  14.  
  15. void _ms2tval __PROTO((unsigned long, struct timeval *));
  16. void _add_tval __PROTO((struct timeval *, struct timeval *));
  17.  
  18. void
  19. _ms2tval(milliseconds, tval)
  20.     unsigned long milliseconds;
  21.     struct timeval *tval;
  22. {
  23.     tval->tv_sec = milliseconds/1000;
  24.     tval->tv_usec = (milliseconds % 1000) * 1000;
  25. }
  26.  
  27. void
  28. _add_tval(orig, new)
  29.     struct timeval *orig, *new;
  30. {
  31.     long t;
  32.  
  33.     t = orig->tv_usec + new->tv_usec;
  34.     if (t > 1000000L) {
  35.         orig->tv_sec += t/1000000L;
  36.         t = t % 1000000L;
  37.     }
  38.     orig->tv_usec = t;
  39.     orig->tv_sec += new->tv_sec;
  40. }
  41.  
  42. int
  43. getrusage(which, data)
  44.     int which;
  45.     struct rusage *data;
  46. {
  47.     long r;
  48.     long usage[8];
  49.  
  50.     _bzero(data, (unsigned long) (sizeof (struct rusage)));
  51.     if (__mint) {
  52.         r = Prusage(usage);
  53.         if (r < 0) {
  54.             errno = (int) -r;
  55.             return -1;
  56.         }
  57.     } else {
  58.         usage[0] = usage[2] = usage[4] = 0;
  59.         usage[1] = _clock() - _childtime;
  60.         usage[3] = _childtime;
  61.     }
  62.  
  63.     if (which == RUSAGE_SELF) {
  64.         _ms2tval(usage[0], &(data->ru_stime));
  65.         _ms2tval(usage[1], &(data->ru_utime));
  66.         data->ru_maxrss = usage[4];
  67.     }
  68.     else if (which == RUSAGE_CHILDREN) {
  69.         _ms2tval(usage[2], &(data->ru_stime));
  70.         _ms2tval(usage[3], &(data->ru_utime));
  71.     }
  72.     return 0;
  73. }
  74.